/******************************************************************************
*  Copyright (C), 2017-2020,Advanced Technology Institute of Suzhou. 
*  THIS IS AN UNPUBLISHED WORK CONTAINING CONFIDENTIAL AND PROPRIETARY 
*   
*******************************************************************************
*  File Name: FundDataHelper.cpp
*  Author: qh.zhang@atisz.ac.cn
*  Version: 1.0.0
*  Date: 2021-1-6
*  Description: brief
*  History: 
******************************************************************************/
#include <QVariant>
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream
#include <QDebug>

#include "FundDataHelper.h"
#include "HttpHelper.h"
FundDataHelper* FundDataHelper::m_pInstance = nullptr;


bool FundDataHelper::getAllFundNames(QMap<QString, QString> &mapAllFundNames)
{
    if (!m_mapAllFundNames.isEmpty())
    {
        mapAllFundNames = m_mapAllFundNames;
        return true;
    }
    if (getAllFundNamesFromSever())
    {
        mapAllFundNames = m_mapAllFundNames;
        return true;
    }
    else
    {
        return false;
    }
}

bool FundDataHelper::getCurInfo(QVector<OneFundInfo> &vecFavFundsInfo)
{
    for (int i = 0 ; i <vecFavFundsInfo.size();i++)
    {
        QString strRes;
        QVariant vStatus;
        //OneFundInfo stOneFundInfo;

        try
        {
            QString strReq = QString("http://fundgz.1234567.com.cn/js/" + QString::fromStdString(vecFavFundsInfo[i].strCode) + ".js");
            HttpHelper::get(strReq, strRes, vStatus);
            strRes = strRes.mid(8, strRes.length() - 10);
            strRes = "{\"OneFundInfo\":" + strRes + "}";
            std::stringstream ss(strRes.toStdString().c_str()); // any stream can be used
            {
                cereal::JSONInputArchive iarchive(ss); // Create an input archive

                iarchive(cereal::make_nvp("OneFundInfo", vecFavFundsInfo[i]));
            }
        }
        catch (...)
        {
            qDebug() << "cereal except"<< vStatus;
            vecFavFundsInfo[i].strLastValue = "Null";
            vecFavFundsInfo[i].strLatestValue = "Null";
            vecFavFundsInfo[i].strName = "Null";
            vecFavFundsInfo[i].strVate = "0";
        }
        

        //std::stringstream ss; // any stream can be used
        //{
        //    cereal::JSONOutputArchive iarchive(ss); // Create an input archive

        //    OneFundInfo stOneFundInfo;
        //    iarchive(cereal::make_nvp("OneFundInfo", stOneFundInfo));

        //}
    }
    return true;
}

bool FundDataHelper::getAllFundNamesFromSever()
{
    QString strRes;
    QVariant vStatus;
    HttpHelper::get(QString("http://fund.eastmoney.com/js/fundcode_search.js"), strRes, vStatus);

    QString strInfo2Split = strRes.mid(strRes.indexOf("[")+2,strRes.length()- strRes.indexOf("[")-4); 
    QStringList lstAllFunNames = strInfo2Split.split("],[");

    if (lstAllFunNames.size()<1000)
    {
        return false;
    }

    for (int i = 0;i<lstAllFunNames.size();i++)
    {
        QStringList lstOneFundNameInfo = lstAllFunNames[i].split(",");
        m_mapAllFundNames[lstOneFundNameInfo[0].remove("\"")] = lstOneFundNameInfo[2].remove("\"");
    }


    return true;
}

